/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * TplFile.h
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#ifndef _TPLFILE_H_
#define _TPLFILE_H_

#include <gctypes.h>
#include <ogc/gx.h>

#define TPL_FMT_CI8		0x01
#define TPL_FMT_RGB		0x02

#define TPL_GC_BANNER	0x00
#define TPL_GC_ICON		0x01
#define TPL_WII_BANNER	0x02
#define TPL_WII_ICON	0x03

#define GC_FRAMEDELAY	4

typedef struct
{
	u32 magic;
	u32 num_textures;
	u32 offset_img_table;
} TPL_FileHeader;

typedef struct
{
	u32 image_header_offset;
	u32 palette_header_offset; //! 0x00 if format RGB
} TPL_ImageOffsetTable;

typedef struct 
{
	u16 entry_count;
	u8  unpacked;
	u8  pad;
	u32 palette_format; //! RGB5A3 = 0x02
	u32 palette_data_offset;
} TPL_PaletteHeader; //! not present in tplHeader if format RGB

typedef struct
{
	u16 height;
	u16 width;
	u32 image_format; //! RGB5A3 = 0x05, CI8 = 0x09
	u32 image_data_offset;
	u32 wrap_s;
	u32 wrap_t;
	u32 min;
	u32 mag;
	f32 lod_bias;
	u8  edge_lod;
	u8  min_lod;
	u8  max_lod;
	u8  unpacked;
} TPL_ImageHeader;

class TplFile
{
public:
	TplFile(u8* mem, u32 imgOffset, u32 palOffset, u8 fmt, u8 type);
	~TplFile();
	
	u32 Size();
	u8* Datas();
	
protected:
	void SetHeader(u8* mem);
	void SetDatas(u8* mem);
	u32 HeaderSize();
	u32 ImgSize();
	u32 PalSize();
	
	u8* data;
	u32 imgOffset;
	u32 palOffset;
	u8 format;
	u8 type;
	
	TPL_FileHeader * fileHeader;
	TPL_ImageOffsetTable * offsetTable;
	TPL_PaletteHeader * paletteHeader;
	TPL_ImageHeader * imageHeader;
};

#endif
